//
//  KKEmployeeEditView.m
//  Butler
//
//  Created by liaozhenming on 16/6/22.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKEmployeeEditView.h"

@interface KKEmployeeEditView()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *numberTextField;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *weixinTextField;

@end

@implementation KKEmployeeEditView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        UIView *contentView = [self contentView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(contentView.bounds), 44)];
        titleLabel.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = @"新增员工";
        [contentView addSubview: titleLabel];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame: CGRectMake(20, 64, 120, 120)];
        headerImageView.backgroundColor = [UIColor yellowColor];
        [contentView addSubview: headerImageView];
        
        CGFloat pointY = 80;
        {
            UILabel *nameTipsLabel = [[UILabel alloc] initWithFrame: CGRectMake(150, pointY, 100, 20)];
            nameTipsLabel.backgroundColor = [UIColor clearColor];
            nameTipsLabel.text = @"员工工号:";
            nameTipsLabel.textColor = [UIColor kk_colorWithHexString: @"555555"];
            [contentView addSubview: nameTipsLabel];
            
            UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(180, pointY + 25, CGRectGetWidth(contentView.bounds) - 200, 26)];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.delegate = self;
            [contentView addSubview: textField];
            _numberTextField = textField;
            pointY += 60;
        }
        {
            UILabel *nameTipsLabel = [[UILabel alloc] initWithFrame: CGRectMake(150, pointY, 100, 20)];
            nameTipsLabel.backgroundColor = [UIColor clearColor];
            nameTipsLabel.text = @"员工名称:";
            nameTipsLabel.textColor = [UIColor kk_colorWithHexString: @"555555"];
            [contentView addSubview: nameTipsLabel];
            
            UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(180, pointY + 25, CGRectGetWidth(contentView.bounds) - 200, 26)];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.delegate = self;
            [contentView addSubview: textField];
            _nameTextField = textField;
            pointY += 60;
        }
        
        {
            UILabel *nameTipsLabel = [[UILabel alloc] initWithFrame: CGRectMake(150, pointY, 100, 20)];
            nameTipsLabel.backgroundColor = [UIColor clearColor];
            nameTipsLabel.text = @"联系方式:";
            nameTipsLabel.textColor = [UIColor kk_colorWithHexString: @"555555"];
            [contentView addSubview: nameTipsLabel];
            
            UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(180, pointY + 25, CGRectGetWidth(contentView.bounds) - 200, 26)];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.delegate = self;
            textField.kk_keyboardEvent = YES;
            [contentView addSubview: textField];
            _phoneTextField = textField;
            pointY += 60;
        }
        
        {
            UILabel *nameTipsLabel = [[UILabel alloc] initWithFrame: CGRectMake(150, pointY, 100, 20)];
            nameTipsLabel.backgroundColor = [UIColor clearColor];
            nameTipsLabel.text = @"微信号:";
            nameTipsLabel.textColor = [UIColor kk_colorWithHexString: @"555555"];
            [contentView addSubview: nameTipsLabel];
            
            UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(180, pointY + 25, CGRectGetWidth(contentView.bounds) - 200, 26)];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.delegate = self;
            textField.kk_keyboardEvent = YES;
            [contentView addSubview: textField];
            _weixinTextField = textField;
            pointY += 60;
        }
        
//        {
//            UILabel *nameTipsLabel = [[UILabel alloc] initWithFrame: CGRectMake(150, pointY, 100, 20)];
//            nameTipsLabel.backgroundColor = [UIColor clearColor];
//            nameTipsLabel.text = @"员工等级:";
//            nameTipsLabel.textColor = [UIColor kk_colorWithHexString: @"555555"];
//            [contentView addSubview: nameTipsLabel];
//            
//            UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(180, pointY + 25, CGRectGetWidth(contentView.bounds) - 200, 26)];
//            textField.borderStyle = UITextBorderStyleRoundedRect;
//            [contentView addSubview: textField];
//            pointY += 60;
//        }
        
        
        UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeSystem];
        cancelButton.frame = CGRectMake(30, CGRectGetHeight(contentView.bounds) - 50, 120, 34);
        cancelButton.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        [cancelButton setTitle: @"取消" forState: UIControlStateNormal];
        [cancelButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [cancelButton addTarget: self action: @selector(action_cancel:) forControlEvents: UIControlEventTouchUpInside];
        [cancelButton kk_cornerRadius: 3.0];
        [contentView addSubview: cancelButton];
        
        UIButton *sureButton = [UIButton buttonWithType: UIButtonTypeSystem];
        sureButton.frame = CGRectMake(CGRectGetWidth(contentView.bounds) - 150, CGRectGetHeight(contentView.bounds) - 50, 120, 34);
        sureButton.backgroundColor = [UIColor kk_colorWithHexString: @"71d47c"];
        [sureButton setTitle: @"确定" forState: UIControlStateNormal];
        [sureButton setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
        [sureButton addTarget: self action: @selector(action_save:) forControlEvents: UIControlEventTouchUpInside];
        [sureButton kk_cornerRadius: 3.0];
        [contentView addSubview: sureButton];
    }
    return self;
}

#pragma mark - Action methods

- (void)action_cancel:(UIBarButtonItem*)item {
    
    [self kk_close];
}

- (void)action_save:(UIBarButtonItem*)item {
    
    NSString *number = _numberTextField.text;
    NSString *name = _nameTextField.text;
    NSString *phone = _phoneTextField.text;
    NSString *weixin = _weixinTextField.text;
    
    NSString *error = nil;
    if (number.length == 0) {
        error = @"工号为空";
    }
    if (name.length == 0) {
        error = @"名称为空";
    }
    if (phone.length == 0) {
        error = @"联系方式为空";
    }
    if (weixin.length == 0) {
        error = @"微信为空";
    }
    
    if (error) {
        [[KKResultAlertView sharedInstance] alertViewShowMessage: error andWithSuccessful: false andWithCallBack: nil];
        return;
    }
    
    [[KKDataBaseManager defaultManager] insertRecordIntoTableName: KKDataBase_TableName_EmployeeInfo withRecords: @{@"number":number,@"name":name,@"phone":phone,@"weixin":weixin} finishHandle:^(id object, BOOL successful) {
        NSLog(@"object = %@",object);
    }];
    
    [self kk_close];
}

@end
